package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.entity.Video;
import com.atguigu.guli.service.edu.mapper.ChapterMapper;
import com.atguigu.guli.service.edu.service.ChapterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-06-24
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private ChapterMapper chapterMapper;

    // 查询课程下所有的章节信息
    @Override
    public List<Chapter> getChapterAll(String courseId) {
        QueryWrapper<Chapter> queryWrapper  = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        return chapterMapper.selectList(queryWrapper);
    }

    // 删除章, 如果章下面有小节则不能删除
    @Override
    public void delChapterById(String chapterId) {

        // 查询章下有没有小节
        List<Video> videoList = chapterMapper.getVideo(chapterId);
        if (videoList.size() > 0) {
            throw new GuliException(ResultCodeEnum.FILE_DELETE_ERROR);
            // return R.error().message("不能删除章");
        }

        // 章下面没有小节, 执行删除
        chapterMapper.deleteById(chapterId);
    }

    // 查询所有的章和小节
    @Override
    public List<Chapter> getChaptersAndVideoS(String courseId) {
        return chapterMapper.getChaptersAndVideoS(courseId);
    }
}
